package com.example.actordemo1.routering;

import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.routing.ActorRefRoutee;
import akka.routing.RoundRobinRoutingLogic;
import akka.routing.Routee;
import akka.routing.Router;
import com.example.actordemo1.becom.BecomActor;

import java.util.ArrayList;
import java.util.List;

public class RouterTaskActor extends AbstractActor {

    private Router router;

    @Override
    public void preStart() throws Exception {
        List<Routee> routees = new ArrayList<>();
        for (int i = 0 ;i<3;i++){
            ActorRef actorRef = getContext().actorOf(Props.create(RouterActor.class), "routerActor" + i);
            routees.add(new ActorRefRoutee(actorRef));
        }
        router = new Router(new RoundRobinRoutingLogic(),routees);
    }

    @Override
    public Receive createReceive() {
        return receiveBuilder().matchAny(o->{
            router.route(o,getSender());
        }).build();
    }

    public static void main(String[] args) {
        ActorSystem actorSystem = ActorSystem.create("sys");
        ActorRef actorRef = actorSystem.actorOf(Props.create(RouterTaskActor.class), "routerTaskActor");
        actorRef.tell("helloA",ActorRef.noSender());
        actorRef.tell("hellob",ActorRef.noSender());
        actorRef.tell("helloc",ActorRef.noSender());
        actorRef.tell("helloe",ActorRef.noSender());
        actorRef.tell("hellof",ActorRef.noSender());
        actorRef.tell("hellog",ActorRef.noSender());
    }
}
